package io.renren.modules.jsf.controller;

import java.util.*;
import java.util.stream.Collectors;

import io.renren.modules.jsf.entity.JsfTotalMoneyEntity;
import io.renren.modules.jsf.entity.JsfYejiEntity;
import io.renren.modules.jsf.entity.SubjectEntity;
import io.renren.modules.jsf.service.JsfTotalMoneyService;
import io.renren.modules.jsf.service.JsfYejiService;
import io.renren.modules.jsf.service.SubjectService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.jsf.entity.YuyueEntity;
import io.renren.modules.jsf.service.YuyueService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserId;


/**
 * @author chen
 * @email sun@gmail.com
 * @date 2022-04-03 14:14:06
 */
@RestController
@RequestMapping("jsf/yuyue")
public class YuyueController {
    @Autowired
    private YuyueService yuyueService;

    @Autowired
    SysUserService userService;

    @Autowired
    SubjectService subjectService;

    @Autowired
    SysUserRoleService roleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jsf:yuyue:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = yuyueService.queryPage(params);
        if (getUserId() != 1) {
            SysUserEntity userEntity = userService.getById(getUserId());
            SysUserRoleEntity userRoleEntity = roleService.selectOne(getUserId());
            List<YuyueEntity> list = (List<YuyueEntity>) page.getList();
            if (userRoleEntity.getRoleId() == 1) {
                //项目自己查看
                List<YuyueEntity> collect = list.stream().filter(t -> {
                    return t.getTeachername().equals(userEntity.getUsername());
                }).collect(Collectors.toList());

                page.setList(collect);
            } else {
                //用户预约
                List<YuyueEntity> collect = list.stream().filter(t -> {
                    return t.getUsername().equals(userEntity.getUsername());
                }).collect(Collectors.toList());

                page.setList(collect);
            }
        }

        //管理员所有
        return R.ok().put("page", page);
    }

    @RequestMapping("/teacher")
    public R teacher(@RequestParam Map<String, Object> params) {

        String code = UUID.randomUUID().toString().substring(0, 10);

        return R.ok().put("teacher", code);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("jsf:yuyue:info")
    public R info(@PathVariable("id") Integer id) {
        YuyueEntity yuyue = yuyueService.getById(id);

        return R.ok().put("yuyue", yuyue);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jsf:yuyue:save")
    public R save(@RequestBody YuyueEntity yuyue) {
        SysUserEntity userEntity = userService.selectOneByName(yuyue.getUsername());
        if (getUserId() != 1) {
            if (!userEntity.getUserId().equals(getUserId())) {
                return R.error("非用户本人操作！");
            }
        }
        if (userEntity == null) {
            return R.error("用户不存在！");
        }

        SubjectEntity subjectEntity = subjectService.getById(yuyue.getCoursename());
        if (subjectEntity == null) {
            return R.error("项目不存在！");
        }

        //入库收支
        JsfTotalMoneyEntity totalMoneyEntity = new JsfTotalMoneyEntity();
        totalMoneyEntity.setUserId(Math.toIntExact(userEntity.getUserId()));
        totalMoneyEntity.setUsername(userEntity.getUsername());
        totalMoneyEntity.setShouRu(String.valueOf(subjectEntity.getSellingprice()));
        totalMoneyEntity.setMemo("用户购买" + subjectEntity.getSubname() + "项目");
        totalMoneyEntity.setCreateTime(new Date());
        totalMoneyService.save(totalMoneyEntity);

        //入库业绩
        JsfYejiEntity yejiEntity = new JsfYejiEntity();
        yejiEntity.setUserId(Math.toIntExact(userEntity.getUserId()));
        yejiEntity.setUsername(userEntity.getUsername());
        yejiEntity.setCoursename(subjectEntity.getSubname());
        yejiEntity.setMoney(String.valueOf(subjectEntity.getSellingprice()));
        yejiEntity.setTeacherName(subjectEntity.getSubname());
        yejiEntity.setCreateTime(new Date());
        yejiService.save(yejiEntity);

        //预约入库
        yuyue.setCreatetime(new Date());
        yuyue.setCoursename(yuyue.getCoursename());
        yuyue.setMoney(subjectEntity.getSellingprice());
        yuyue.setTeachername(subjectEntity.getSubname());
        yuyueService.save(yuyue);

        return R.ok();
    }

    @Autowired
    JsfTotalMoneyService totalMoneyService;

    @Autowired
    JsfYejiService yejiService;

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jsf:yuyue:update")
    public R update(@RequestBody YuyueEntity yuyue) {
        yuyueService.updateById(yuyue);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jsf:yuyue:delete")
    public R delete(@RequestBody Integer[] ids) {
        yuyueService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
